package com.itany.boot.controller;

import com.itany.boot.constant.ResponseCodeConstant;
import com.itany.boot.entity.Chapter;
import com.itany.boot.exception.ChapterExistException;
import com.itany.boot.exception.FileUploadErrorException;
import com.itany.boot.exception.RecourceExistException;
import com.itany.boot.exception.RequestParameterErrorException;
import com.itany.boot.service.ChapterService;
import com.itany.boot.service.ResourceService;
import com.itany.boot.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * @date 2025/10/17
 */
@RestController
@RequestMapping("/chapter")
public class ChapterController {

    @Autowired
    private ChapterService chapterService;
    @Autowired
    private ResourceService resourceService;

    @RequestMapping("/findAll")
    public ModelAndView findAll(){
        ModelAndView mav = new ModelAndView();

        mav.setViewName("back_courseReourceSet");
        return mav;
    }

    @RequestMapping("/addChapter")
    public ResponseResult addChapter(String title, String inFo, CommonsMultipartFile file, String rtitle, String costType, String costNumber, String fileType, HttpSession session){
        ResponseResult result = new ResponseResult();

        try {
            chapterService.addChapter(title,inFo);
            chapterService.addResource(file,rtitle,costType,costNumber,fileType,session);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (ChapterExistException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (RecourceExistException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }
}
